package com.zjcode.cdcer.controller;

import com.chinatechstar.component.commons.controller.BaseController;
import com.chinatechstar.component.commons.result.ListResult;
import com.chinatechstar.component.commons.result.ResultBuilder;
import com.chinatechstar.component.commons.supports.PageData;
import com.zjcode.cdcer.model.CerDemoInfo;
import com.zjcode.cdcer.model.basic.JsonObj;
import com.zjcode.cdcer.service.CerDemoInfoService;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName:SystemInitController
 * @功能：系统初始
 * @author：wlei
 * @日期：2022/7/9-02:00
 **/
@RestController
@Slf4j
@RequestMapping("/v1/systeminit/")
public class SystemInitController extends BaseController {

    @Autowired
    private CerDemoInfoService cerDemoInfoService;



    /**
     * 分页获取数据
     *
     * @return
     */
    @PostMapping("listByPage")
    public ListResult<Object> listByPage(@RequestBody PageData pageData) {

        int serverPort = this
            .getRequest()
            .getServerPort();

        log.info("serverPort: {}", serverPort);

        Map<String, Object> map = cerDemoInfoService.listByPage(pageData);
        return ResultBuilder.buildListSuccess(map);
    }

    /**
     * 测试数据请求
     *
     * @param request
     * @return
     */
    @GetMapping("sayHi")
    public JsonObj sayHi(HttpServletRequest request) {
        int serverPort = request.getServerPort();
        PageData pageData = getPageData();
        String message = (String) pageData.get("message");

        Map<String, Object> data = new HashMap<>();
        data.put("测试", "1.0.0");
        data.put("serverPort", serverPort);
        data.put("message", message);

        return JsonObj.success(data);
    }



    /**
     * 数据插入演示
     *
     * @return
     */
    @PostMapping("execInsert")
    public JsonObj execInsert(@RequestBody PageData pageData) {

        int serverPort = this
            .getRequest()
            .getServerPort();

        log.info("serverPort: {}", serverPort);

        String remarks = (String) pageData.get("remarks");

        // 默认一次性插入3条记录
        // 执行插入(使用mybatis plus)
        int count = 0;
        for (int i = 0; i < 3; i++) {
            CerDemoInfo cerDemoInfo = new CerDemoInfo();
            cerDemoInfo.setRemarks(remarks + "," + LocalDateTime
                .now()
                .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            cerDemoInfo.setInsertDatetime(LocalDateTime.now());
            cerDemoInfo.setLastUpdateDatetime(LocalDateTime.now());
            cerDemoInfo.setDeleted(i % 2);
            cerDemoInfo.setVersion(1);
            count += cerDemoInfoService.insert(cerDemoInfo);
        }
        return JsonObj.success("插入成功:" + count + "," + LocalDateTime.now());
    }

    /**
     * 数据查询演示
     *
     * @return
     */
    @PostMapping("execSelect")
    public JsonObj execSelect(@RequestBody PageData pageData) {

        int serverPort = this
            .getRequest()
            .getServerPort();

        log.info("serverPort: {}", serverPort);

        // 执行查询(使用mybatis plus)
        List<CerDemoInfo> list = cerDemoInfoService.listAllNoPage();
        log.info("执行查询(使用mybatis plus):" + list);

        // 执行查询(使用自定义sql)
        List<CerDemoInfo> list1 = cerDemoInfoService.selectAllDeleteData();
        log.info("执行查询(使用自定义sql):" + list1);

        Map<String, Object> map = new HashMap<>();
        map.put("tag1", list);
        map.put("tag2", list1);
        return JsonObj.success(map);
    }

    /**
     * 分页获取测试数据
     *
     * @return
     */
    @PostMapping("testData")
    public ListResult<Object> testData(@RequestBody PageData pageData) {

        //int serverPort = this
        //        .getRequest()
        //        .getServerPort();
        //
        //log.info("serverPort: {}", serverPort);

        Map<String, Object> map = cerDemoInfoService.getAllTestData(pageData);
        return ResultBuilder.buildListSuccess(map);
    }
}
